/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.views
{
	import com.puny.utils.ObjectFinder;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Rectangle;

	/**
	 * MessageBoxView: A message box which has at least one button. 
	 * @author puny
	 * @example
	 * MyAsset.swf should have MyMessageBoxAsset, MyButtonAsset1, and MyButtonAsset2". All the three assets should be exported as its name.
	 * MyMessageBoxAsset should contain MyButtonAsset1 and MyButtonAsset2 on itself as well.
	 * <listing version="3.0">
	 * var dobj:DisplayObject = addChild(new MessageBoxView("MyAsset.swf", "MyMessageBoxAsset", "MyButtonAsset1", "MyButtonAsset2"))
	 * dobj.addEventListener("MyButtonAsset1, buttonHandler);
	 * dobj.addEventListener("MyButtonAsset2, buttonHandler);
	 * 
	 * private function buttonHandler(event:Envet):void {
	 * 	trace(event.type, "button is clicked");
	 * }
	 * </listing>
	 */
	public class MessageBoxView extends ResizeHandlerButtonView implements IResizedHandler
	{
		private var _assetName:String;
		protected var _buttons:Array;
		protected var _mc:MovieClip;
		/**
		 * Overlapped window with buttons. If "open" or "close" label is defined,
		 * It will be played. When one of buttons is clicked, this window will be closed.
		 * An event which has type as same name as the exported name will be dispatched as well.
		 * 
		 * This windows will be placed at center of the parent window.
		 * If the parent or itself　has mask in themselves, the window position might be off.
		 * 
		 * @param swf swf class, swf instance, or file name of swf.
		 * @param assetName exported asset name
		 * @param args exported butotn names in "assetName" asset
		 * 
		 */
		public function MessageBoxView(swf:Object,assetName:String, ... args)
		{
			_assetName = assetName;
			_buttons = args;
			super(swf);
		}
		
		/**
		 * @private 
		 * 
		 */
		override protected function setup():void {
			super.setup();
			_mc = getMovieClip(_assetName);
			_mc.visible = false;
//			for(var i:int = 0;i < _mc.numChildren;i++) {
//				trace(_mc.getChildAt(i));
//			}		
		
			for each(var buttonName:String in _buttons) {
				addButtonListeners(ObjectFinder.find(_mc, buttonName) as MovieClip, closeHandler);
			}
			
			var rect:Rectangle = _mc.getBounds(this);
			_mc.x = -rect.x;
			_mc.y = -rect.y;
		}
		
		/**
		 * @private 
		 * 
		 */
		override public function beforeReady():void {
			super.beforeReady();
			resizedHandler(null);
			_mc.visible = true;
		}
		
		/**
		 * @private 
		 * 
		 */
		override public function beforeOpen():void {
			super.beforeOpen();
			_mc.gotoAndStop("open");
			resizedHandler(null);
		}
		
		/**
		 * @private 
		 * 
		 */
		override public function procedureOpen():Boolean {
			super.procedureOpen();
			_mc.nextFrame();
			resizedHandler(null);
			return _mc.currentLabel == "open" && _mc.currentFrame < _mc.totalFrames;
		}
		
		/**
		 * @private 
		 * 
		 */
		override public function afterOpen():void {
			super.afterOpen();
			_mc.stop();
		}
				
		/**
		 * @private 
		 * 
		 */
		override public function beforeClose():void {
			super.beforeClose();
			_mc.gotoAndStop("close");
			resizedHandler(null);
		}
		
		/**
		 * @private 
		 * 
		 */
		override public function procedureClose():Boolean {
			super.procedureClose();
			_mc.nextFrame();
			resizedHandler(null);
			return _mc.currentLabel == "close" && _mc.currentFrame < _mc.totalFrames;
		}
				
		/**
		 * @private 
		 * 
		 */
		private function closeHandler(event:Event):void {
			dispatchEvent(new Event(ObjectFinder.eliminateObjectName(event.currentTarget.toString())));
			complete();
		}
		
		/**
		 * This windows will be placed at center of the parent window.
		 * If the parent or itself　has mask in themselves, the window position might be off.
		 * 
		 * @param event
		 * 
		 */
		public function resizedHandler(event:Event):void {
			_mc.x = (parent.width - _mc.width) / 2;
			_mc.y = (parent.height - _mc.height) / 2;
		}
	}
}